// pages/productDetail/productDetail.js
const app = getApp();
Page({
  /**
   * 页面的初始数据
   */
  data: {
    //图标
    iconPath: app.globalData.iconPath,
    //访问java后台
    goJavaBackstagePath: app.globalData.goJavaBackstagePath,
    //测试数据图片
    testDataImgPath: app.globalData.testDataImgPath,

    product:{},
    iconList:{},
    pre: 1,
    photoCount: '',//用户预览选取的图片总数
    currentPhoto: '',//用户预览选取的图片   当前预览的第几张
    photoViewPath: '',//当前预览图片的路径 

    orderId:null,
    productId:null,
    orderStatus:null,
  },
  //缩略图删除--------------------
  deleteThumbnail(event){
    var deletePhotoList = this.data.product;
    var productPhotoId = event.target.dataset.productphotoid;
    console.log(productPhotoId)
    //传到后台删除该产品下对应的图片
    // wx.request({
    //     url: this.data.goJavaBackstagePath+'/product/deletePhoto',
    //     data:{productPhotoId:productPhotoId},
    //     header:{'content-type': 'application/json'},
    //     success:function(res){
    //       console.log(res.data);
    //     }
    // })
    deletePhotoList.photos.splice(event.target.dataset.index,1);
    this.setData({
      product: deletePhotoList
    });
    console.log(this.data.product)
  },

  //系统自带图片预览
  previewimg(event){
    wx.previewImage({
      current: event.target.dataset.url,
      urls: this.data.product.photos,
    })
  },
  
  //自定义图片预览
  previewPhoto(event){
    this.setData({
      pre:0,
      photoCount: this.data.product.photos.length,
      currentPhoto: event.target.dataset.index+1,
      photoViewPath: this.data.product.photos[event.target.dataset.index].url    
    });
  },

  //退出自定义图片预览
  outPre(event){
    this.setData({
      pre: 1,
      photoCount: '',
      currentPhoto: '',
      photoViewPath: '',
    });
  },

  //上一张
  goPrevious(event){
      if(this.data.currentPhoto==1) { 
        wx.showModal({
          title: '提示',
          content: '这是第一张图片!',
        })
      }else{
        this.setData({
          currentPhoto:this.data.currentPhoto-1,
          photoViewPath: this.data.product.photos[this.data.currentPhoto - 2].url
        });
      } 
  },

  //下一张
  goNext(event){
    if (this.data.currentPhoto == this.data.photoCount){
      wx.showModal({
        title: '提示',
        content: '这是最后一张图片!',
      })
    }else{
      this.setData({
        currentPhoto:this.data.currentPhoto+1,
        photoViewPath: this.data.product.photos[this.data.currentPhoto].url
      });
    }
  },

  //删除已选取的图片
  deletePhoto(event){
    var wasDeleteProduct = this.data.product;
    if (this.data.photoCount==1){
      //到后台删除图片
      // wx.request({
      //     url: this.data.goJavaBackstagePath+'/product/deletePhoto',
      //     data: { orderId: this.data.orderId, productId: this.data.productId, productPhotoId: wasDeleteProduct.photos[this.data.currentPhoto - 1].productPhoto_id},
      //     header:{'content-type': 'application/json'},
      //     success:function(res){
      //       console.log(res.data);
      //     }
      // })
      wasDeleteProduct.photos.splice(this.data.currentPhoto - 1, 1);
      this.setData({
        product: wasDeleteProduct,      
      });
      this.outPre();//退出预览
      return;
    }
     //到后台删除图片
      // wx.request({
      //     url: this.data.goJavaBackstagePath+'/product/deletePhoto',
      //     data: { orderId: this.data.orderId, productId: this.data.productId, productPhotoId: wasDeleteProduct.photos[this.data.currentPhoto - 1].productPhoto_id},
      //     header:{'content-type': 'application/json'},
      //     success:function(res){
      //       console.log(res.data);
      //     }
      // })
    wasDeleteProduct.photos.splice(this.data.currentPhoto - 1,1);
    this.setData({
      product: wasDeleteProduct,
      photoViewPath: this.data.product.photos[this.data.currentPhoto-1].url,
      photoCount:this.data.product.photos.length
    });
  },
  //跳转到选片页面
  goPhotoRepository(event){
    var that = this;
    if(this.data.orderStatus==0){
      wx.showModal({
        title: '提示',
        content: '照片未上传，无法进入选片！',
      })
    }else if(2>this.data.orderStatus>0){
      wx.redirectTo({
        url: '/pages/photoRepository/photoRepository?orderId=' + this.data.orderId + '&productId=' + this.data.productId +'&orderStatus='+this.data.orderStatus,
      })
    }
  },

  
// 底部菜单----------------------------
  //跳转到订单列表
  goOrderList(event){
    var that = this;
    wx.redirectTo({
      url: '../order/order',
    })
  },

  //添加当前产品数量
  addProductCount(event){
    if(this.data.orderStatus>=2){
      wx.showModal({
        title: '提示',
        content: '当前订单已结存，无法添加商品数量！',
      })
    }else{
      this.data.product.productCount++;
      var productCount = this.data.product.productCount;
      this.setData({
        product: this.data.product,
      });
      //发送到后台添加
      // wx.request({
      //   url: this.data.goJavaBackstagePath+'/product/addProductCount',
      //   data:{orderId:this.data.orderId,productId:this.data.productId},
      //   header:{'content-type': 'application/json'},
      //   success:function(res){
      //     console.log(res.data);
      //   }
      // })
    }
  },
  //减少当前产品数量----此处删除不合理，用户选取保存以后下次进入无法删除
  deleteProductCount(event){
    if(this.data.orderStatus>=2){
      wx.showModal({
        title: '提示',
        content: '当前订单已结存，无法减少商品数量！',
      })
    }else{
      if (this.data.product.productCount == this.data.product.orderProductCount) {
        wx.showModal({
          title: '提示',
          content: '当前数量为下单时数量，无法删除！',
        })
        return ;
      } else if (this.data.product.productCount > this.data.product.orderProductCount) {
        if ((this.data.product.productCount - 1) * this.data.product.oneProductCount<this.product.photos.length){
          wx.showModal({
            title: '提示',
            content: '您选的照片过多无法删除，请先删除部分已选照片！',
          })
        }else{
          this.data.product.productCount--;
          var productCount = this.data.product.productCount;
          this.setData({
            product: this.data.product,
          });
        //请求到后台 减少产品数量，如果产品为自选产品 最终可以删除产品
        // wx.request({
        //   url: this.data.goJavaBackstagePath+'/product/deleteProductCount',
        //   data:{orderId:this.data.orderId,productId:this.data.productId},
        //   header:{'content-type': 'application/json'},
        //   success:function(res){
        //     console.log(res.data);
        //   }
        // })
        }        
      }   
    }
    
  },
  //跳转到产品列表页面
  goProductList(event){
    wx.redirectTo({
      url: '../product/product?orderId='+this.data.orderId+'&orderStatus='+this.data.orderStatus
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var orderId = options.orderId;
    var productId = options.productId;
    var orderStatus = options.orderStatus;
    this.setData({
      orderId:orderId,
      productId:productId,
      orderStatus: orderStatus
    })
    //获取产品详情页面的数据
    // wx.request({
    //   url: this.data.goJavaBackstagePath+'/product/getProductDetail',
    //   data: { orderId: this.data.orderId, productId: this.data.productId },
    //   header: {
    //     'content-type': 'application/json'
    //   },
    //   success: function (res) {
    //     console.log(res.data)
    //   }
    // })

    //从后获取当前产品的信息
    var iconList= {
      "out": this.data.iconPath + '/out.png',
      'selectadd': this.data.iconPath + '/selectadd.png',
      'selectdelete': this.data.iconPath + '/selectdelete.png',
      "pressDelete": this.data.iconPath + '/pressDelete.png',
      "delete": this.data.iconPath + '/delete.png',
      "pressAdd": this.data.iconPath + '/pressAdd.png',
      "jenter": this.data.iconPath + '/enter.png',
      'jdown': this.data.iconPath + '/down.png',
      "menuadd": this.data.iconPath + '/menuadd.png',
      'smallDelete': this.data.iconPath + '/smallDelete.png',
      'smallnotDelete': this.data.iconPath + '/smallnotDelete.png',
      'order': this.data.iconPath + '/order.png',
      'product': this.data.iconPath + '/product.png',
      'menudelete': this.data.iconPath + '/menudelete.png',
    };
    var product = {
      //orderProductCount:下单时产品数量   productCount：产品总数量
      'id': 22, 'orderProductCount':6, 'productCount': 6, 'oneProductCount': 5,'name': '实木相框挂墙16寸',  'url':this.data.iconPath + '/sellerportrait.png', 'photos': [
        { id: 1,productPhoto_id:1,url:this.data.testDataImgPath+ '/mei1.jpg'},
        { id: 2, productPhoto_id: 2,url:this.data.testDataImgPath+ '/mei2.jpg'},
        { id: 3, productPhoto_id: 3,url:this.data.testDataImgPath+ '/mei3.jpg'},
        { id: 4, productPhoto_id: 4,url:this.data.testDataImgPath+ '/mei4.jpg'},
        { id: 5, productPhoto_id: 5,url: this.data.testDataImgPath+ '/mei5.jpg'},
        { id: 6, productPhoto_id: 6,url: this.data.testDataImgPath+ '/mei6.jpg'},
        { id: 7, productPhoto_id: 7,url: this.data.testDataImgPath+ '/mei7.jpg'},
        { id: 8, productPhoto_id: 8,url: this.data.testDataImgPath+ '/mei8.jpg'},
        { id: 9, productPhoto_id: 9,url: this.data.testDataImgPath+ '/mei9.jpg'},
        { id: 10, productPhoto_id: 10,url: this.data.testDataImgPath+ '/mei10.jpg'},
        { id: 11, productPhoto_id: 11,url: this.data.testDataImgPath+ '/mei11.jpg'},
        { id: 12, productPhoto_id: 12,url: this.data.testDataImgPath+ '/mei12.jpg'},
        { id: 13, productPhoto_id: 13,url: this.data.testDataImgPath+ '/mei13.jpg'},
        { id: 14, productPhoto_id: 14,url: this.data.testDataImgPath+ '/mei14.jpg'},
        { id: 15, productPhoto_id: 15,url: this.data.testDataImgPath+ '/mei15.jpg'},
        { id: 16, productPhoto_id: 16,url: this.data.testDataImgPath+ '/mei16.jpg'},
      ]
    };

    this.setData({
      
      product: product,
      iconList:iconList,
     
    });
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
   
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
  
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
  
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
  
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  }
})